/**
 * Copyright (C), 1968-2019, 美的集团股份有限公司
 * FileName: UserController
 * Author:   fanyl5
 * Date:     2019/4/20 16:28
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ruiysoft.crm.controller;

import cn.taroco.common.constants.CommonConstant;
import cn.taroco.common.web.Response;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.crm.model.dto.DeptAndUserTree;
import com.ruiysoft.crm.model.dto.UserDTO;
import com.ruiysoft.crm.model.entity.SysDept;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.service.SysDeptService;
import com.ruiysoft.crm.service.SysUserService;
import com.ruiysoft.crm.util.TreeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: fanyl5
 * @date: 2019/4/20 16:28
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    SysDeptService sysDeptService;

    @Autowired
    SysUserService sysUserService;

    /**
     * 获取组织机构及组织机构成员树
     *
     * @param deptId
     * @return
     */
    @GetMapping("/userWithDept")
    public Response queryUserWithDept(@RequestParam(value = "deptId", required = false) Integer deptId) {
        SysDept deptCondition = new SysDept();
        deptCondition.setDelFlag(CommonConstant.STATUS_NORMAL);
        List<SysDept> depts = sysDeptService.list(new QueryWrapper<>(deptCondition));

        SysUser userCondition = new SysUser();
        userCondition.setDelFlag(CommonConstant.STATUS_NORMAL);
        List<SysUser> sysUsers = sysUserService.list(new QueryWrapper<>(userCondition));
        List<UserDTO> users = new ArrayList<>();
        sysUsers.forEach(sysUser -> {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(sysUser, userDTO);
            users.add(userDTO);
        });

        //不传deptId,默认使用顶级deptId
        if (ObjectUtils.isEmpty(deptId)) {
            deptId = 0;
        }

        List<DeptAndUserTree> trees = new ArrayList<>();
        DeptAndUserTree node;
        for (SysDept dept : depts) {
            if (dept.getParentId().equals(dept.getDeptId())) {
                continue;
            }
            node = new DeptAndUserTree();
            node.setId(dept.getDeptId());
            node.setParentId(dept.getParentId());
            node.setDeptName(dept.getName());

            List<UserDTO> userList = new ArrayList<>();
            for (UserDTO user : users) {
                if (user.getDeptId().equals(dept.getDeptId())) {
                    userList.add(user);
                }
            }
            node.setUserList(userList);

            trees.add(node);
        }
        List<DeptAndUserTree> result = TreeUtil.buildByRecursive(trees, deptId);
        return Response.success(result);
    }

    /**
     * 根据部门分组用户数据
     *
     * @return Response
     */
    @GetMapping("/getUsersByDept")
    public Response getUsersByDept() {
        return Response.success(sysUserService.getUsersByDept());
    }

}
